﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeastSquareMethod
{
    internal class FunctionOfRegression
    {
        public static double function(Points[] GDP, int year)
        {//我需要得到平均的X,Y
            double averageX = 0;
            double averageY = 0;
            //这个foreach的后缀会自动生成
            foreach (Points p in GDP)
            {
                averageX += p.X;
                averageY += p.Y;
            }
            averageX /= GDP.Length;
            averageY /= GDP.Length;
            //现在需要的是斜率b，denominator分母，分子numerator
            double b, numerator, denominator;
            numerator = 0;
            denominator = 0;
            foreach (Points p in GDP)
            {
                numerator += (p.X - averageX) * (p.Y - averageY);
                denominator += (p.X - averageX) * (p.X - averageX);
            }
            b = numerator / denominator;
            double a = averageY - b * averageX;
            //求完所有之后，返回预测值
            return a + b * year;
        }
    }
}

